package gdut.jmxywz.action;

import gdut.jmxywz.been.Message;
import gdut.jmxywz.been.MessageFiles;
import gdut.jmxywz.been.MessageMembers;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class MessageAction extends BaseAction {
	
	//获取登录信息
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	
	private Message           message ;
	
	private MessageMembers    messageMembers;
	
	private MessageFiles      messageFiles;
	
	private Teacher          teacher;
	
	private  List<Teacher>   teacherList;
	
	private  List<MessageMembers>  messageMembersList;
	//文件
	private String[] files_content;
	private String[] files_title;
	private List<File> upload_file;
	private String[] files_save_as;
	private String[] upload_fileContentType;
	private String[] upload_fileFileName;
	private String   savePath;
    
	private String    receive;
	private List<MessageFiles>  messageFileList;
	
	
	//分页
	private int        currentPage=1;
	private int        pageSize=5;
	private String     nextPage;
	private PageModel  pm;
	
	//查询条件
	private String     sender;
	private String     receiver;
	private String     title; 
	private String     startTime;  
	private String     endTime;
	private String     state;
	private String     searchWhere="" ;
	
	private String     where_search;
	private String     where_value;
	private String     like;
	//回复转发
	private String   reply;
	private String   resend;
	private String   replyOrResend;   
	

	
	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public MessageMembers getMessageMembers() {
		return messageMembers;
	}

	public void setMessageMembers(MessageMembers messageMembers) {
		this.messageMembers = messageMembers;
	}

	public MessageFiles getMessageFiles() {
		return messageFiles;
	}

	public void setMessageFiles(MessageFiles messageFiles) {
		this.messageFiles = messageFiles;
	}

	public List<MessageFiles> getMessageFileList() {
		return messageFileList;
	}

	public void setMessageFileList(List<MessageFiles> messageFileList) {
		this.messageFileList = messageFileList;
	}
    
	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public String[] getFiles_content() {
		return files_content;
	}

	public void setFiles_content(String[] filesContent) {
		files_content = filesContent;
	}

	public String[] getFiles_title() {
		return files_title;
	}

	public void setFiles_title(String[] filesTitle) {
		files_title = filesTitle;
	}

	public List<File> getUpload_file() {
		return upload_file;
	}

	public void setUpload_file(List<File> uploadFile) {
		upload_file = uploadFile;
	}

	public String[] getFiles_save_as() {
		return files_save_as;
	}

	public void setFiles_save_as(String[] filesSaveAs) {
		files_save_as = filesSaveAs;
	}

	public String[] getUpload_fileContentType() {
		return upload_fileContentType;
	}

	public void setUpload_fileContentType(String[] uploadFileContentType) {
		upload_fileContentType = uploadFileContentType;
	}

	public String[] getUpload_fileFileName() {
		return upload_fileFileName;
	}

	public void setUpload_fileFileName(String[] uploadFileFileName) {
		upload_fileFileName = uploadFileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getNextPage() {
		return nextPage;
	}

	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}
	

	public String getSender() {
		return sender;
	}

	public void setSender(String sender)throws UnsupportedEncodingException  {
		this.sender =new String(sender.getBytes("ISO-8859-1"),"UTF-8");
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) throws UnsupportedEncodingException {
		this.receiver = new String(receiver.getBytes("ISO-8859-1"),"UTF-8");
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title)throws UnsupportedEncodingException  {
		this.title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime)throws UnsupportedEncodingException   {
		this.startTime = new String(startTime.getBytes("ISO-8859-1"),"UTF-8");
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime)throws UnsupportedEncodingException   {
		this.endTime = new String(endTime.getBytes("ISO-8859-1"),"UTF-8");
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getSearchWhere() {
		return searchWhere;
	}

	public void setSearchWhere(String searchWhere) throws UnsupportedEncodingException  {
		this.searchWhere = new String (searchWhere.getBytes("ISO-8859-1"),"UTF-8");
	}
	
	

	public String getReply() {
		return reply;
	}

	public void setReply(String reply) {
		this.reply = reply;
	}

	public String getResend() {
		return resend;
	}

	public void setResend(String resend) {
		this.resend = resend;
	}
	

	public String getReplyOrResend() {
		return replyOrResend;
	}

	public void setReplyOrResend(String replyOrResend) {
		this.replyOrResend = replyOrResend;
	}
	

	public List<Teacher> getTeacherList() {
		return teacherList;
	}

	public void setTeacherList(List<Teacher> teacherList) {
		this.teacherList = teacherList;
	}

	public String getWhere_search() {
		return where_search;
	}

	public void setWhere_search(String where_search) {
		this.where_search = where_search;
	}

	public String getWhere_value() {
		return where_value;
	}

	public void setWhere_value(String where_value) throws UnsupportedEncodingException {
		this.where_value =new String(where_value.getBytes("ISO-8859-1"),"utf-8");
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getReceive() {
		return receive;
	}

	public void setReceive(String receive) {
		this.receive = receive;
	}

	public List<MessageMembers> getMessageMembersList() {
		return messageMembersList;
	}

	public void setMessageMembersList(List<MessageMembers> messageMembersList) {
		this.messageMembersList = messageMembersList;
	}

	/***
	 *  发送消息
	 * @return
	 * @throws Exception
	 */
	public String Add() throws Exception{
		
		teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		
		System.out.println("----"+teacher.getTeacherName());
		
		//发送者
		message.setTeacher(teacher);
		
		
	    List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		
		List recieveList = new ArrayList();
		setSavePath("");//上传路径
		
		boolean  b = false;
		boolean  b2 =false;
		System.out.println(receive.indexOf("("));
		if(receive.indexOf("(")!=-1){
			int start=0;
			int end = 1;
			while(true){
				 start=receive.indexOf("(");
				 end =receive.indexOf(")");
				 if(start!=end){
				    String  name = receive.substring(start+1, end);
				    receive=receive.substring(end+1, receive.length()).toString();
				    recieveList.add(name);
				 }else{
					 break;
				 }
			}
		}
		
		//将消息存入消息列表
		b=this.getCommonService().saveObject(message);
		
		System.out.println("------"+message.getMessageTitle());
		
		//存入消息接受者表
		Iterator ite = recieveList.iterator();
		while(ite.hasNext()){
			String rname = (String)ite.next();
			String hql = "from Teacher where teacherName = '"+rname+"'";
			
			System.out.println("-------"+hql);
			
			List list = this.getCommonService().findListByHql(hql, null);
			
			Iterator iter=list.iterator();
			
			while(iter.hasNext()){
				
			   Teacher  teacher1 = (Teacher)iter.next();
			   
			   System.out.println("-----"+teacher1.getTeacherName());
			   //存入消息接受者表
				MessageMembers mm = new MessageMembers();
				
				mm.setMessage(message);
				mm.setTeacher(teacher1);
			    mm.setState("0");
				this.getCommonService().saveObject(mm);
			}
			
			System.out.println("-"+rname);
		}
		
		//处理文件上传

		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分

				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\messageFiles\\"+extend_name;//文件在服务器保存的相对路径
				
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
			    MessageFiles mfile = new MessageFiles();
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				mfile.setFileUrl(Save_path);
				mfile.setFileName(files_title[i]);
				mfile.setFileContext(files_content[i]);
				mfile.setMessage(message) ;
				
				//插入附件表
				b2=this.getCommonService().saveObject(mfile);
		
		//文件上传处理结束
		
     	}
		
		if(b){
			return "MessageAdd";
		}else{
			return "error";
		}
	
	}
	
	/***
	 *  显示个人所有帖子--管理员
	 * @return
	 * @throws Exception
	 */
	public String ListAll()throws Exception{
		
		String hql ="from Message  m where";
		
		nextPage = "tecmessageListAll.action";
		
       System.out.println("====="+hql);
       
       String str = this.Search();
       
       int i =str.indexOf("@");
       
       hql =hql+str.substring(0,i);
       
       searchWhere = str.substring(i+1, str.length());
       
       System.out.println("--"+hql);
       
       System.out.println("--"+searchWhere);
		
		pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, searchWhere);
		
		
		return "MessageListAll";
	}
	
	/***
	 * 留言详细
	 * @return
	 * @throws Exception
	 */
	 public String Detail() throws Exception{
		 

		 System.out.println("--id--"+message.getMessageId()+"+++++++++++++++++++++++++++++++++");
		 
		 String hql ="from MessageFiles mf where mf.message.messageId="+message.getMessageId();
		 
		 String hql2="from MessageMembers  mm where mm.message.messageId="+message.getMessageId(); 
		 
		 message=(Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
		 
		 System.out.println(message.getMessageContext());
		 
		 System.out.println("----"+hql2);
		 //判断是否有附件
           		 
		 messageFileList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator ite = messageFileList.iterator();
		 
		 while(ite.hasNext()){
			 
			 MessageFiles mf = (MessageFiles)ite.next();
			 
			 System.out.println("++++++"+mf.getFileName());
			 
		 }
		 
//		 //该消息是发给谁的。
		 
		 messageMembersList = this.getCommonService().findListByHql(hql2, null);
		 
		 Iterator iter = messageMembersList.iterator();
		 while(iter.hasNext()){
			 
			 MessageMembers messageMembers = (MessageMembers)iter.next();
			 System.out.println(messageMembers.getTeacher().getTeacherName());
			 
		 }

		 return "MessageDetail";
		 
	 }
	 
	 /***
	  * 
	  *  查看个人收信箱
	  * @return
	  * @throws Exception
	  */
	 public String ListReceive() throws Exception{
		 
      teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		 
		 System.out.println("===="+teacher.getTeacherName());
		 System.out.println("----"+teacher.getTeacherId());
		 
		 
		 String hql ="from Message m where m.messageId in(select mm.message.messageId from MessageMembers mm where mm.teacher.teacherId="+teacher.getTeacherId()+") and ";
		 
	     nextPage = "tecmessageListReceive.action";
		 	  
		 	 
	     System.out.println("--hql--"+hql);
	     
	     
	     String str = this.Search();
	       
	       int i =str.indexOf("@");
	       
	       hql =hql+str.substring(0,i);
	       
	       searchWhere = str.substring(i+1, str.length());
	       
	      
	       System.out.println("--"+hql);
	       
	       System.out.println("--"+searchWhere);
	       
 		 pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, searchWhere);
		 
 		 if(pm!=null&&pm.getList()!=null&&pm.getList().size()>0){
 			 
		  Iterator iter = pm.getList().iterator();
		  
		  while(iter.hasNext()){
			  Message m = (Message)iter.next();
			  
			  System.out.println(m.getMessageContext());
		  }
 		 }
		 
		 return "MessageListReceive";
	 }
	 
	 /***
	  *  查看本人已发送消息
	  * @return
	  * @throws Exception
	  */
	 public String ListSelf() throws Exception{
		 
		 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		 
		 System.out.println("===="+teacher.getTeacherName());
		 
		 System.out.println("----"+teacher.getTeacherId());
		 
		 String hql="from Message  m  where m.teacher.teacherId="+teacher.getTeacherId()+" and ";
		 
	     nextPage = "tecmessageListSelf.action";
	     
	     String str = this.Search();
	     
	     int i =str.indexOf("@");
	       
	     hql =hql+str.substring(0,i);
	     
	     searchWhere = str.substring(i+1, str.length());
	       
	     System.out.println("--"+hql);
	       
	     System.out.println("--"+searchWhere);
	     
         System.out.println("--hql--"+hql);
		
		 pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, searchWhere);
		 
		 if(pm!=null&&pm.getList()!=null){
			 
	      List  list = pm.getList();
		 
		  Iterator iter = list.iterator();
		  
		  while(iter.hasNext()){
			  Message m = (Message)iter.next();
			  
			  System.out.println(m.getMessageContext());
		  }
		 }
		return "MessageListSelf"; 
	 }
	 
	 
	 /***
	  * 
	  * 回复转发
	  * @return
	  * @throws Exception
	  */
     public String ReplyOrResend() throws Exception{
		 

		 System.out.println("--id--"+message.getMessageId());
		 
		 if("1".equals(reply)){
			 
			 replyOrResend = "1";
			 
		 }
		 if("1".equals(resend)){
			 
			 replyOrResend = "0";
		 }
		 
		 String hql ="from MessageFiles mf where mf.message.messageId="+message.getMessageId();
		 
		 String hql2="from MessageMembers  mm where mm.message.messageId="+message.getMessageId(); 
		 
		 message=(Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
		 
		 System.out.println(message.getMessageContext());
		 
		 System.out.println("----"+hql2);
		 //判断是否有附件
           		 
		 messageFileList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator ite = messageFileList.iterator();
		 
		 while(ite.hasNext()){
			 
			 MessageFiles mf = (MessageFiles)ite.next();
			 
			 System.out.println("++++++"+mf.getFileName());
			 
		 }
		 
//		 //该消息是发给谁的。
		 
		 
		 List list = this.getCommonService().findListByHql(hql2, null);
		 
		 Iterator iter = list.iterator();
		 
		 while(iter.hasNext()){
			 
		     // 消息为已读
			 
			 messageMembers = (MessageMembers)iter.next(); 
			 
			 System.out.println("-------"+messageMembers.getTeacher().getTeacherName());
			 
			 messageMembers.setState("1");
			 
			 this.getCommonService().updateObject(messageMembers);
			 
			 
		 }

		 
		 return "MessageReplyAndResend";
		 
	 }
     
	 /***
	  * 管理员删除消息
	  * @return
	  * @throws Exception
	  */
	 
     public String Delete() throws Exception{
    	 
    	 System.out.println("-----"+message.getMessageId());
    	 
    	 String hql =" from MessageFiles mf where mf.message.messageId = "+message.getMessageId();
    	 
    	 String hql1 = " from MessageMembers mm where mm.message.messageId ="+message.getMessageId();
    	 
    	 
    	 List fileList = this.getCommonService().findListByHql(hql, null);
    	 
    	 Iterator iter = fileList.iterator();
    	 
    	 //删除附件
    	 while(iter.hasNext()){
    		 
    		 MessageFiles mf =(MessageFiles) iter.next();
    		 
            //删除服务器附件
    		 
			 String path =ServletActionContext.getRequest().getRealPath("/");
			 
			 path = path+mf.getFileUrl();
			 
			 System.out.println("-----------"+path);
    		 
    		 this.deleteFile(path);
    		 
    		 System.out.println("--delete--files--"+mf.getMessageFilesId());
    		 
    		 this.getCommonService().deleteObject(mf);
    		 
    	 }
    	 //删除接收者
    	 List mmList   = this.getCommonService().findListByHql(hql1, null);
    	 
    	 Iterator iter1 = mmList.iterator();
    	 
    	 while(iter1.hasNext()){
    		 
    		 MessageMembers mm = (MessageMembers)iter1.next();
    		 
    		 
    		 System.out.println("--mm---"+mm.getId());
    		 
    		 this.getCommonService().deleteObject(mm);
    	 }
    	 
    	 Message mes = (Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
    	 
    	 this.getCommonService().deleteObject(mes);
    	 
    	 System.out.println("---删除成功---");
    	 
    	 return "MessageDelete";
     }
     
	 /***
	  *删除本人发送的消息
	  * @return
	  * @throws Exception
	  */
	 
     public String DeleteSelf() throws Exception{
    	 
    	 System.out.println("-----"+message.getMessageId());
    	 
    	 String hql =" from MessageFiles mf where mf.message.messageId = "+message.getMessageId();
    	 
    	 String hql1 = " from MessageMembers mm where mm.message.messageId ="+message.getMessageId();
    	 
    	 
    	 List fileList = this.getCommonService().findListByHql(hql, null);
    	 
    	 Iterator iter = fileList.iterator();
    	 
    	 //删除附件
    	 while(iter.hasNext()){
    		 
    		 MessageFiles mf =(MessageFiles) iter.next();
    		 
            //删除服务器附件
    		 
			 String path =ServletActionContext.getRequest().getRealPath("/");
			 
			 path = path+mf.getFileUrl();
			 
			 System.out.println("-----------"+path);
    		 
    		 this.deleteFile(path);
    		 
    		 System.out.println("--delete--files--"+mf.getMessageFilesId());
    		 
    		 this.getCommonService().deleteObject(mf);
    		 
    	 }
    	 //删除接收者
    	 List mmList   = this.getCommonService().findListByHql(hql1, null);
    	 
    	 Iterator iter1 = mmList.iterator();
    	 
    	 while(iter1.hasNext()){
    		 
    		 MessageMembers mm = (MessageMembers)iter1.next();
    		 
    		 
    		 System.out.println("--mm---"+mm.getId());
    		 
    		 this.getCommonService().deleteObject(mm);
    	 }
    	 
    	 Message mes = (Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
    	 
    	 this.getCommonService().deleteObject(mes);
    	 
    	 System.out.println("---删除成功---");
    	 
    	 return "MessageDeleteSelf";
     }
	 /***
	  *删除本人接受的消息
	  * @return
	  * @throws Exception
	  */
	 
     public String DeleteReceive() throws Exception{
    	 
    	 System.out.println("-----"+message.getMessageId());
    	 
    	 String hql =" from MessageFiles mf where mf.message.messageId = "+message.getMessageId();
    	 
    	 String hql1 = " from MessageMembers mm where mm.message.messageId ="+message.getMessageId();
    	 
    	 
    	 List fileList = this.getCommonService().findListByHql(hql, null);
    	 
    	 Iterator iter = fileList.iterator();
    	 
    	 //删除附件
    	 while(iter.hasNext()){
    		 
    		 MessageFiles mf =(MessageFiles) iter.next();
    		 
            //删除服务器附件
    		 
			 String path =ServletActionContext.getRequest().getRealPath("/");
			 
			 path = path+mf.getFileUrl();
			 
			 System.out.println("-----------"+path);
    		 
    		 this.deleteFile(path);
    		 
    		 System.out.println("--delete--files--"+mf.getMessageFilesId());
    		 
    		 this.getCommonService().deleteObject(mf);
    		 
    	 }
    	 //删除接收者
    	 List mmList   = this.getCommonService().findListByHql(hql1, null);
    	 
    	 Iterator iter1 = mmList.iterator();
    	 
    	 while(iter1.hasNext()){
    		 
    		 MessageMembers mm = (MessageMembers)iter1.next();
    		 
    		 
    		 System.out.println("--mm---"+mm.getId());
    		 
    		 this.getCommonService().deleteObject(mm);
    	 }
    	 
    	 Message mes = (Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
    	 
    	 this.getCommonService().deleteObject(mes);
    	 
    	 System.out.println("---删除成功---");
    	 
    	 return "MessageDeleteReceive";
     }
     
     
     /***
      * 消息检索
      * @return
      * @throws Exception
      */

     public String  Search(){
    	 
//    	 teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
    	 
    	 String hql = "";
    	 
    	 String str = "";
    	 
    	 //拼接查询条件
    	 if(!"".equals(sender)&&sender!=null){
    		 hql = hql+" m.teacher.teacherName like '"+sender+"'" ;
    		 
    		 searchWhere =searchWhere+"&sender="+sender;
    		 
    	 }else {
    		 hql = hql +" 1 = 1";
    	 }
    	
    	 if(!"".equals(receiver)&&receiver!=null){
        	 hql = hql+" and m.messageId in(select mm.message.messageId from MessageMembers mm where mm.teacher.teacherName like '"+receiver+"' )";

        	 searchWhere=searchWhere+"&"+"receiver="+receiver;
        	 
        	 System.out.println("searchWhere=--"+searchWhere);
    	 }
    	 
    	 if(!"".equals(title)&&title!=null){
        	 hql = hql+" and m.messageTitle like '%"+title+"%' ";
        	 searchWhere=searchWhere+"&"+"title="+title;

    	 }
    	 
    	 if(!"".equals(startTime)&&startTime!=null){
        	 hql = hql+" and m.messageTime >= '"+startTime+"'";
        	 
        	 searchWhere=searchWhere+"&"+"startTime="+startTime;
    	 }
    	 if(!"".equals(endTime)&&endTime!=null){
        	 hql = hql+" and m.messageTime <= '"+endTime+" '";
        
        	 searchWhere=searchWhere+"&endTime="+endTime+"&";
    	 }
    	 if(!"".equals(state)&&state!=null){

        	 hql = hql+" and m.messageId in(select mm.message.messageId from MessageMembers mm where mm.state= '"+state+"')";
        
        	 searchWhere=searchWhere+"&"+"state="+state;
    	 }
    	 
    	 if(sender==""&&receiver==""&&title==""&&startTime==""&&endTime==""&&state==""){
    		 
    		 hql=hql+" 1=1 ";
    	 }
    	 hql = hql +" order by messageTime desc,messageId desc";
    	 
    	 
    	 System.out.println("hql=="+hql);
    	 
    	 System.out.println("searchWhere=--"+searchWhere);
//    	 
//    	 pm = this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, searchWhere);
    	 
    	 str = hql+"@"+searchWhere;
    	
    	 return str;
     }
     /***
      * 查找教师信息
      * @return
      * @throws Exception
      */
     public String SearchTeacher() throws Exception{
    	 
    	String hql ="from Teacher where 1=1 ";
   	  	String  search_parameter = "";
   	  	String en_where_value="";
    	 
    	 if(!"".equals(where_value)&&where_value!=null){
    		  System.out.println("+++++++++++++++++++++++++++++");
    		  en_where_value=java.net.URLEncoder.encode(where_value, "utf-8");//URL编码转换
    		  System.out.println("++++-- "+where_value);
    		  
    		  if("like".equals(like))
    		  {
             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
    		  }else if("and".equals(like)){
    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
    		  }
    		  
    	  }
    	 hql = hql+" order by teacherId asc";
    	 //查询条件
   	    search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
   	  
   	    System.out.println("=============="+search_parameter);
    	 nextPage ="tecmessageSearchTeacher.action";
    	 
    	 pageSize = 10;
    	 
    	 pm = this.getCommonService().findPage(hql, null, currentPage, pageSize,nextPage, search_parameter);
    	 
    	 return "MessageSearchTeacher";
     }
     /***
 	 *  接受消息详细
 	 * @return
 	 * @throws Exception
 	 */
 	 public String DetailReceive() throws Exception{
 		 
 		 System.out.println("--id--"+message.getMessageId());
 		 
 		 String hql ="from MessageFiles mf where mf.message.messageId="+message.getMessageId();
 		 
 		 String hql2="from MessageMembers  mm where mm.message.messageId="+message.getMessageId(); 
 		 
 		 message=(Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
 		 
 		 System.out.println(message.getMessageContext());
 		 
 		 
 		 //判断是否有附件
            		 
 		 messageFileList = this.getCommonService().findListByHql(hql, null);
 		 
 		 Iterator ite = messageFileList.iterator();
 		 
 		 while(ite.hasNext()){
 			 
 			 MessageFiles mf = (MessageFiles)ite.next();
 			 
 			 System.out.println("++++++"+mf.getFileName());
 			 
 		 }
 		 
// 		 //该消息是发给谁的。
 		 
 		 messageMembersList = this.getCommonService().findListByHql(hql2, null);
 		 
// 		 Iterator iter = messageMembersList.iterator();
// 		 while(iter.hasNext()){
// 			 MessageMembers messageMembers = (MessageMembers)iter.next();
// 			 System.out.println(messageMembers.getTeacher().getTeacherName());
// 		 }


 		 return "MessageDetailReceive";
 		 
 	 }
 	/***
  	 *  发送消息xiang细
  	 * @return
  	 * @throws Exception
  	 */
  	 public String DetailSelf() throws Exception{
  		 
  		 System.out.println("--id--"+message.getMessageId());
  		 
  		 String hql ="from MessageFiles mf where mf.message.messageId="+message.getMessageId();
  		 
  		 String hql2="from MessageMembers  mm where mm.message.messageId="+message.getMessageId(); 
  		 
  		 message=(Message)this.getCommonService().findByIntId(message.getMessageId(), "gdut.jmxywz.been.Message");
  		 
  		 System.out.println(message.getMessageContext());
  		 
  		 System.out.println("----"+hql2);
  		 //判断是否有附件
             		 
  		 messageFileList = this.getCommonService().findListByHql(hql, null);
  		 
  		 Iterator ite = messageFileList.iterator();
  		 
  		 while(ite.hasNext()){
  			 
  			 MessageFiles mf = (MessageFiles)ite.next();
  			 
  			 System.out.println("++++++"+mf.getFileName());
  			 
  		 }
  		 
//  		 //该消息是发给谁的。
  		 
  		 messageMembersList = this.getCommonService().findListByHql(hql2, null);
//  		 Iterator iter = messageMembersList.iterator();
//  		 while(iter.hasNext()){
//  			 MessageMembers messageMembers = (MessageMembers)iter.next();
//  			 System.out.println(messageMembers.getTeacher().getTeacherName());
//  		 }


  		 return "MessageDetailSelf";
  		 
  	 }
     
     /**
	    *  deleteFile  从服务器上删除附件
	    * @param sPath
	    * @return
	    */
		
     public  boolean deleteFile(String sPath) {   
		    boolean  flag = false;   
			File   file = new File(sPath);   
		    // 路径为文件且不为空则进行删除   
		    if (file.isFile() && file.exists()) {   
		        file.delete();   
		        System.out.println("---成功从服务器删除文件---");
		        flag = true;   
		    }   
		    return flag;   
		}  
     
     
     
	
	 
}
	 
	
   
	    
